package com.cloudspace.jindun.activity.dianhua;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.error.VolleyError;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.adapter.EmergencyPersonAdatper;
import com.cloudspace.jindun.model.ContactPersonListResult;
import com.cloudspace.jindun.model.Person;
import com.cloudspace.jindun.network.api.API;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.CallingUtils;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.List;

/**
 * Created by plato on 2016/12/24.
 */

public class EmergencyActivity extends CustomTitleBaseActivity {
    private ListView listView;
    private EmergencyPersonAdatper adatper;

    private List<Person> persons;

    public static final String TAG = EmergencyActivity.class.getSimpleName();

    private void getEmergencyContactPersonData(){
        API.getInstance().getOrgApi().getEmergencyContactPersons(TAG, UserSessionManager.getInstance().getUserId(), new ApiCallback<ContactPersonListResult>() {
            @Override
            public void handleResult(ContactPersonListResult result, VolleyError error) {
                if (!ObjUtil.isEmpty(result) && error == null){
                    persons = result.data;
                    adatper.setData(persons);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getEmergencyContactPersonData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_emergency);
        getTitleBar().setSimpleMode(getResources().getString(R.string.emergency_contract));

        listView = (ListView) findViewById(R.id.phone_listview_emergency);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!ObjUtil.isEmpty(persons) && !ObjUtil.isEmpty(persons.get(position))){
                    Person person = persons.get(position);
                    CallingUtils.showCallMethodDialog(EmergencyActivity.this, person);
                }
            }
        });
        adatper = new EmergencyPersonAdatper(this);
        listView.setAdapter(adatper);
    }
}
